/*  Novelties: A visual novel programming language based on JavaFX
    Copyright (C) 2008  Hao Xu

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * /
 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avgengine.script;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ertri
 */
public class TextMetrics {

    public TextMetrics(int numline, int maxWidth) {
        this.numline = numline;
        this.maxWidth = maxWidth;
    }
    List<String> loglist = new ArrayList<String>();
    int numline;
    int maxWidth;
    int currentline = 0;
    int current = 0;
    List<Marker> paragraphDivider = new ArrayList<Marker>();

    public void appendText(Font f, String actor, String text) {
        FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
        List<String> slist = new ArrayList<String>();
        int offset = 0;
        int p = 0;

        while (p < text.length()) {
            while (p < text.length() && fm.stringWidth(text.substring(offset, p)) < maxWidth) {
                p++;
            }
            slist.add(text.substring(offset, p));
            offset = p;
        }

        paragraphDivider.add(new Marker(actor, loglist.size()));
        loglist.addAll(slist);

    }

    public String getFullPage() {
        StringBuffer sbuf = new StringBuffer();
        int n = Math.min(current + numline - currentline, loglist.size());

        for (int i = current; i < n; i++) {
            String s = loglist.get(i);

            sbuf.append(s);
            sbuf.append('\n');

        }
        int d = n - current;
        current += d;
        currentline += d;
        currentline %= numline;
        return sbuf.toString();
    }

    public boolean hasMore() {
        return this.current < loglist.size();
    }

    public void reset() {
        currentline = 0;
    }

    public static class Marker {

        String actor;
        int line;

        public Marker(String a, int l) {
            this.actor = a;
            this.line = l;
        }
    }
}

